package com.another.helper;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.URL;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.widget.Toast;
import cn.pedant.SweetAlert.SweetAlertDialog;

public class InstallAPK extends AsyncTask<String, Void, Void>{
	
	private SweetAlertDialog dialog; 
	private SweetAlertDialog errorDialog;
	private int status = 0;
	private Context context;
	private String errorMessage;
	
	public InstallAPK(Context context, SweetAlertDialog dialog) {
		this.context  = context; 
		this.dialog	  = dialog; 
	}
	
	public void setErrorDialog(SweetAlertDialog errorDialog){
		this.errorDialog = errorDialog;
	}
	
	@Override
	protected void onPreExecute() {
		dialog.show();
	}
	
	@Override
	protected Void doInBackground(String... params) {
		errorMessage = null;
		try {
			URL url = new URL(params[0]); 
			HttpURLConnection c = (HttpURLConnection) url.openConnection(); 
			c.setRequestMethod("GET");
			c.setDoInput(true); 
			c.connect();
			
			File sdcard = Environment.getExternalStorageDirectory(); 
			File myDir  = new File(sdcard, "Android/data/com.nusaraya.simpd/update"); 
			myDir.mkdirs(); 
			File outputFile = new File(myDir, "simpd.apk"); 
			if(outputFile.exists()){
				outputFile.delete(); 
			}
			
			
			FileOutputStream fos = new FileOutputStream(outputFile); 
			
			InputStream is = c.getInputStream(); 
			byte[] buffer = new byte[1024]; 
			int len1 = 0; 
			while ((len1 = is.read(buffer)) != -1) {
				fos.write(buffer, 0, len1);
			}
			
			fos.flush(); 
			fos.close(); 
			is.close(); 
			
			Intent intent = new Intent(Intent.ACTION_VIEW);
			intent.setDataAndType(Uri.fromFile(new File(sdcard, "Android/data/com.nusaraya.simpd/update/simpd.apk")), 
					"application/vnd.android.package-archive");
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
			context.startActivity(intent);
			/*File file = new File(sdcard, "Android/data/com.nusaraya.simpd/update/simpd.apk");
			if(file.exists()){
				try {
					String command; 
					command = "adb install -r "+file.getAbsolutePath();
					Process proc = Runtime.getRuntime().exec(new String[] { "su", "-c", command });
					proc.waitFor();
				} catch (Exception e) {
					
				}
			}*/
			
			
		}catch (ConnectException e) {
			Logger.e(context, "update apk error", e.toString());
			errorMessage = "tidak bisa terhubung dengan server!";
		}
		catch (FileNotFoundException e) {
			Logger.e(context, "update apk error", e.toString());
			errorMessage = "update apk error!";
			e.printStackTrace();
		} catch (IOException e) {
			Logger.e(context, "update apk error", e.toString());
			errorMessage = "update apk error!";
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	protected void onPostExecute(Void unused) {
		dialog.dismiss(); 
		if(status == 1){
			Toast.makeText(context, "Updates not available ", Toast.LENGTH_SHORT).show();
		}
		if(errorMessage != null){
			errorDialog.setTitleText("Oops...!!"); 
			errorDialog.setContentText(errorMessage);
			errorDialog.show();
			
		}
	}
	
}
